package cn.kgc.tongxunlu.service;

import cn.kgc.tongxunlu.mapper.ContactMapper;
import cn.kgc.tongxunlu.pojo.Contact;
import cn.kgc.tongxunlu.pojo.ContactExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-28 9:16
 */
@Service
public class ContactServiceImpl implements ContactService {

        @Resource
    ContactMapper contactMapper;

    @Override
    public PageInfo<Contact> pageInfo(Integer pageNum, Integer pageSize, String cname) {
        PageHelper.startPage(pageNum,pageSize);
        ContactExample example=new ContactExample();
        ContactExample.Criteria criteria = example.createCriteria();

        if(cname==null){
            List<Contact> contacts = contactMapper.selectByExample(null);
            PageInfo<Contact> pageInfo=new PageInfo<>(contacts);
            return pageInfo;
        }else{
            criteria.andCnameLike("%"+cname+"%");
            List<Contact> contacts = contactMapper.selectByExample(example);
            PageInfo<Contact> pageInfo=new PageInfo<>(contacts);
            return pageInfo;
        }
    }

    @Override
    public Integer add(Contact contact) {
        return contactMapper.insert(contact);
    }

    @Override
    public Contact selectById(Integer id) {
        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(Contact contact) {
        return contactMapper.updateByPrimaryKeySelective(contact);
    }
}
